﻿using BCCommon;
using BCData.MySql.MaterialConfiguration;
using BCDto.MySql.MaterialConfiguration;
using BCEntity.MySql.MaterialConfiguration;
using BCService.Common.WebsiteConfigurationService;
using BCService.Utils; 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.WebDomainService.MaterialConfigurationService
{
    /// <summary>
    /// 素材服务
    /// </summary>
    public class MaterialConfigurationService : IMaterialConfigurationService
    {
        private readonly IMaterialConfigurationData materialConfigurationData;
        private readonly IWebsiteConfigurationService websiteConfigurationService;
        public MaterialConfigurationService(IMaterialConfigurationData materialConfigurationData,
            IWebsiteConfigurationService websiteConfigurationService)
        {
            this.materialConfigurationData = materialConfigurationData;
            this.websiteConfigurationService = websiteConfigurationService;
        }

        public MaterialConfigurationDto Create(MaterialConfigurationRequestDto materialConfigurationRequestDto, string editor)
        {
            if (materialConfigurationRequestDto == null)
            {
                throw new ArgumentException("添加的素材信息不能为空");
            }
            if (string.IsNullOrWhiteSpace(editor))
            {
                throw new ArgumentException("修改人不能为空");
            }
            if (this.materialConfigurationData.Exists(materialConfigurationRequestDto.MaterialName))
            {
                throw new ArgumentException("素材信息已存在，无法重复添加。");
            }
            MaterialConfigurationEntity entity = materialConfigurationRequestDto.As<MaterialConfigurationEntity>();
            entity.Editer = editor;
            var result = this.materialConfigurationData.Create(entity);
            if (result == null)
            {
                return null;
            }
            return result.As<MaterialConfigurationDto>();
        }

        public int Delete(int materialConfigurationId)
        {
            if (!this.materialConfigurationData.Exists(materialConfigurationId))
            {
                throw new ArgumentException("素材信息不存在");
            }
            return this.materialConfigurationData.Delete(materialConfigurationId);
        }

        public MaterialConfigurationDto Get(int materialConfigurationId)
        {
            return this.materialConfigurationData.Get(materialConfigurationId).As<MaterialConfigurationDto>();
        }
        public MaterialConfigurationDto Get(string searchKey) {
            return this.materialConfigurationData.Get(searchKey).As<MaterialConfigurationDto>();
        }

        public PageableList<MaterialConfigurationDto> List(string materialName, string materialType, string remark, string usageGroupCode, DateTime? start, DateTime? end, bool? ascending, int pageIndex = 1, int pageSize = 10)
        {
            var list = this.materialConfigurationData.List(materialName, materialType, remark, usageGroupCode, start, end, ascending, out int dataCout, pageIndex, pageSize);
            //string host = GeneralContainer.Configuration["CurrentlyImageHost"];

            var dtos = list.Select(p => p.As<MaterialConfigurationDto>());
            foreach(var item in dtos)
            {
                item.FullAddress = item.Address;
            }
            return new PageableList<MaterialConfigurationDto>
            {
                Count = dataCout,
                Items = dtos
            };
        }

        public MaterialConfigurationDto Update(int materialConfigurationId, MaterialConfigurationSimPutDto materialConfigurationSimPutDto, string editor)
        {
            var materialConfigurationEntity = this.materialConfigurationData.Get(materialConfigurationId);
            if (materialConfigurationEntity == null)
            {
                throw new ArgumentException("素材信息不存在");
            }
            if (materialConfigurationSimPutDto == null)
            {
                throw new ArgumentException("修改的素材信息不能为空");
            }
            if (string.IsNullOrWhiteSpace(editor))
            {
                throw new ArgumentException("修改人不能为空");
            }
            materialConfigurationEntity.MaterialName = materialConfigurationSimPutDto.MaterialName;
            materialConfigurationEntity.Content = materialConfigurationSimPutDto.Content;
            materialConfigurationEntity.Remark = materialConfigurationSimPutDto.Remark;
            materialConfigurationEntity.RemarkPhoto = materialConfigurationSimPutDto.RemarkPhoto;
            materialConfigurationEntity.UpdateTime = DateTime.Now;
            materialConfigurationEntity.UsageGroupCode = materialConfigurationSimPutDto.UsageGroupCode;
            materialConfigurationEntity.Address = materialConfigurationSimPutDto.Address;
            materialConfigurationEntity.Editer = editor;
            return this.materialConfigurationData.Update(materialConfigurationEntity).As<MaterialConfigurationDto>();
        }
    }
}